package com.hospital.register.core.controller.website;

import com.hospital.register.common.result.Result;
import com.hospital.register.core.service.HospitalService;
import com.hospital.register.core.service.HospitalSetService;
import com.hospital.register.dto.core.common.HospitalPageDto;
import com.hospital.register.vo.core.admin.HospitalAndRule;
import com.hospital.register.vo.core.admin.HospitalPageVo;
import com.hospital.register.vo.core.api.HospitalSignVo;
import com.hospital.register.vo.core.common.HospitalQueryVo;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "医院信息", description = "面向用户")
@RestController
@RequestMapping("/api/hosp/hospital")
@Validated
public class HospitalSiteController {

    /**
     * 处理医院相关业务
     */
    @Autowired
    private HospitalService hospitalService;

    /**
     * 处理医院设置相关业务
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "分页查询医院列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page", value = "页码", dataTypeClass = Integer.class, paramType = "path", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", dataTypeClass = Integer.class, paramType = "path", required = true)
    })
    @GetMapping("/{page}/{limit}")
    public Result<Page<HospitalPageVo>> findHospList(
            @PathVariable @Min(value = 1, message = "页码不可小于 1") Integer page,
            @PathVariable @Range(min = 1L, max = 50, message = "每页长度必须在1到50之间") Integer limit,
            HospitalPageDto conditions) {
        conditions.setPage(page);
        conditions.setLimit(limit);
        Page<HospitalPageVo> resultData = this.hospitalService.findPage(conditions);
        return Result.ok(resultData);
    }

    @ApiOperation(value = "根据医院名称查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "hosname", value = "医院名称", dataTypeClass = String.class, paramType = "path", required = true)
    })
    @GetMapping("/findByHosName/{hosname}")
    public Result<List<HospitalPageVo>> findByHosName(@PathVariable String hosname) {
        HospitalPageDto conditions = new HospitalPageDto();
        conditions.setHosname(hosname);
        Page<HospitalPageVo> pageData = this.hospitalService.findPage(conditions);
        return Result.ok(pageData.getContent());
    }

    @ApiOperation(value = "根据医院编号获取医院预约挂号详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "hoscode", value = "医院的编号", dataTypeClass = String.class, paramType = "path", required = true)
    })
    @GetMapping("/{hoscode}")
    public Result<HospitalAndRule> findDetail(@PathVariable String hoscode) {
        HospitalQueryVo hospital = this.hospitalService.findByHoscode(hoscode);
        //外面重新包一层
        HospitalAndRule hospitalAndRule = new HospitalAndRule();

        if (hospital != null) {
            hospitalAndRule.setHospital(hospital);
            hospitalAndRule.setBookingRule(hospital.getBookingRule());
            hospital.setBookingRule(null);
        }

        return Result.ok(hospitalAndRule);
    }

    @ApiOperation(value = "获取医院签名信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "hoscode", value = "医院的编号", dataTypeClass = String.class, paramType = "path", required = true)
    })
    @GetMapping("/inner/getSignInfoVo/{hoscode}")
    public HospitalSignVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") @NotBlank(message = "医院编号不可为空") String hoscode) {
        return this.hospitalSetService.selectSignByCode(hoscode);
    }

}
